home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1991 Regents of the University of California */
-
- #ifndef lint
- static char SCCSid[] = "@(#)color.c 2.1 11/12/91 LBL";
- #endif
-
- /*
- * color.c - routines for color calculations.
- *
- * 10/10/85
- */
-
- #include <stdio.h>
-
- #include "color.h"
-
- #define MINELEN 8 /* minimum scanline length for encoding */
- #define MINRUN 4 /* minimum run length */
-
-
- char *tempbuffer(len) /* get a temporary buffer */
- unsigned len;
- {
- extern char *malloc(), *realloc();
- static char *tempbuf = NULL;
- static int tempbuflen = 0;
-
- if (len > tempbuflen) {
- if (tempbuflen > 0)
- tempbuf = realloc(tempbuf, len);
- else
- tempbuf = malloc(len);
- tempbuflen = tempbuf==NULL ? 0 : len;
- }
- return(tempbuf);
- }
-
-
- int fwritecolrs(scanline, len, fp) /* write out a colr scanline */
- register COLR *scanline;
- int len;
- register FILE *fp;
- {
- register int i, j, beg, cnt;
- int c2;
-
- if (len < MINELEN) /* too small to encode */
- return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
- if (len > 32767) /* too big! */
- return(-1);
- putc(2, fp); /* put magic header */
- putc(2, fp);
- putc(len>>8, fp);
- putc(len&255, fp);
- /* put components seperately */
- for (i = 0; i < 4; i++) {
- for (j = 0; j < len; j += cnt) { /* find next run */
- for (beg = j; beg < len; beg += cnt) {
- for (cnt = 1; cnt < 127 && beg+cnt < len &&
- scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
- ;
- if (cnt >= MINRUN)
- break; /* long enough */
- }
- if (beg-j > 1 && beg-j < MINRUN) {
- c2 = j+1;
- while (scanline[c2++][i] == scanline[j][i])
- if (c2 == beg) { /* short run */
- putc(128+beg-j, fp);
- putc(scanline[j][i], fp);
- j = beg;
- break;
- }
- }
- while (j < beg) { /* write out non-run */
- if ((c2 = beg-j) > 128) c2 = 128;
- putc(c2, fp);
- while (c2--)
- putc(scanline[j++][i], fp);
- }
- if (cnt >= MINRUN) { /* write out run */
- putc(128+cnt, fp);
- putc(scanline[beg][i], fp);
- } else
- cnt = 0;
- }
- }
- return(ferror(fp) ? -1 : 0);
- }
-
- int fwritescan(scanline, len, fp) /* write out a scanline */
- register COLOR *scanline;
- int len;
- FILE *fp;
- {
- COLR *clrscan;
- int n;
- register COLR *sp;
- /* get scanline buffer */
- if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
- return(-1);
- clrscan = sp;
- /* convert scanline */
- n = len;
- while (n-- > 0) {
- setcolr(sp[0], scanline[0][RED],
- scanline[0][GRN],
- scanline[0][BLU]);
- scanline++;
- sp++;
- }
- return(fwritecolrs(clrscan, len, fp));
- }
-
- int setcolr(clr, r, g, b) /* assign a short color value */
- register COLR clr;
- double r, g, b;
- {
- double frexp();
- double d;
- int e;
-
- d = r > g ? r : g;
- if (b > d) d = b;
-
- if (d <= 1e-32) {
- clr[RED] = clr[GRN] = clr[BLU] = 0;
- clr[EXP] = 0;
- return;
- }
-
- d = frexp(d, &e) * 256.0 / d;
-
- clr[RED] = r * d;
- clr[GRN] = g * d;
- clr[BLU] = b * d;
- clr[EXP] = e + COLXS;
- }
- /* Copyright (c) 1991 Regents of the University of California */
-
- /*
- * header.c - routines for reading and writing information headers.
- *
- * 8/19/88
- *
- * printargs(ac,av,fp) print an argument list to fp, followed by '\n'
- * isformat(s) returns true if s is of the form "FORMAT=*"
- * formatval(r,s) copy the format value in s to r
- * fputformat(s,fp) write "FORMAT=%s" to fp
- * getheader(fp,f,p) read header from fp, calling f(s,p) on each line
- * checkheader(i,p,o) check header format from i against p and copy to o
- *
- * To copy header from input to output, use getheader(fin, fputs, fout)
- */
-
- #include <stdio.h>
- #include <ctype.h>
-
- #define MAXLINE 512
-
- char FMTSTR[] = "FORMAT=";
- int FMTSTRL = 7;
-
-
- int fputformat(s, fp) /* put out a format value */
- char *s;
- FILE *fp;
- {
- fputs(FMTSTR, fp);
- fputs(s, fp);
- putc('\n', fp);
- }
-
-